#!/usr/bin/env bash
# Reference: https://github.com/ibraheemdev/modern-unix

TARGET=${TARGET:-/usr/local}
BASH_COMPLETION_D=${BASH_COMPLETION_D:-/etc/bash_completion.d}

PWD=$(dirname "$0")
DL_CACHE=$PWD/pkgs

install_tar() {
	local FN=$1
	local STRIP=$2
	local EXE=$3
	local COMPLETION
	rm -rf /tmp/$FN
	mkdir -p /tmp/$FN
	tar -xf $DL_CACHE/$FN -C /tmp/$FN --strip-components=$STRIP
	mv /tmp/$FN/$EXE $TARGET/bin
	if [ $# -ge 4 ]; then
		COMPLETION=$4
		mv /tmp/$FN/$COMPLETION $BASH_COMPLETION_D/
	fi
	rm -rf /tmp/$FN
}

install_zip() {
	local FN=$1
	local EXE=$2
	local COMPLETION
	rm -rf /tmp/$FN
	mkdir -p /tmp/$FN
	unzip $DL_CACHE/$FN -d /tmp/$FN
	mv /tmp/$FN/$EXE $TARGET/bin
	if [ $# -ge 3 ]; then
		COMPLETION=$3
		mv /tmp/$FN/$COMPLETION $BASH_COMPLETION_D/
	fi
	rm -rf /tmp/$FN
}

remove() {
	local EXE=$1
	local COMPLETION1
	local COMPLETION2
	rm -f $TARGET/bin/$EXE
	if [ $# -ge 2 ]; then
		COMPLETION1=$2
		rm -f $BASH_COMPLETION_D/$COMPLETION1
	fi
	if [ $# -ge 3 ]; then
		COMPLETION2=$3
		rm -f $BASH_COMPLETION_D/$COMPLETION2
	fi
}

# fzf: fuzzy finder (and ctrl-r)
FZF_VER=0.27.2
FZF_BIN_FN=fzf-${FZF_VER}-linux_amd64.tar.gz
FZF_BIN_URL="https://github.com/junegunn/fzf/releases/download/${FZF_VER}/${FZF_BIN_FN}"
FZF_SRC_FN=fzf-${FZF_VER}.tar.gz
FZF_SRC_URL="https://github.com/junegunn/fzf/archive/refs/tags/${FZF_VER}.tar.gz"

download_fzf() {
	wget -c -O $DL_CACHE/$FZF_BIN_FN "$FZF_BIN_URL"
	wget -c -O $DL_CACHE/$FZF_SRC_FN "$FZF_SRC_URL"
}
clean_fzf() {
	rm -f $DL_CACHE/$FZF_BIN_FN
	rm -f $DL_CACHE/$FZF_SRC_FN
}
install_fzf() {
	rm -rf $TARGET/src/fzf
	mkdir -p $TARGET/src/fzf
	tar -xf $DL_CACHE/$FZF_SRC_FN -C $TARGET/src/fzf --strip-components=1
	tar -xf $DL_CACHE/$FZF_BIN_FN -C $TARGET/src/fzf/bin
}
init_fzf() {
	$TARGET/src/fzf/install --all
}
remove_fzf() {
	$TARGET/src/fzf/uninstall
	rm -rf $TARGET/src/fzf
}

# lsd: ls
LSD_VER=0.20.1
LSD_FN=lsd-${LSD_VER}-x86_64-unknown-linux-musl.tar.gz
LSD_URL="https://github.com/Peltoche/lsd/releases/download/${LSD_VER}/${LSD_FN}"

download_lsd() {
	wget -c -O $DL_CACHE/$LSD_FN "$LSD_URL"
}
clean_lsd() {
	rm -f $DL_CACHE/$LSD_FN
}
install_lsd() {
	install_tar $LSD_FN 1 lsd autocomplete/lsd.bash-completion
	lsd /
}
init_lsd() {
	rm -rf $HOME/.config/lsd
	mkdir -p $HOME/.config/lsd
	{
		echo "icons:"
		echo "  when: never"
	} >> $HOME/.config/lsd/config.yaml
	{
		echo "alias ls=lsd"
		echo "alias l=ls"
		echo "alias la='ls -a'"
		echo "alias ll='ls -l'"
		echo "alias lla='ls -la'"
	} >> $HOME/.bashrc
}
remove_lsd() {
	remove lsd lsd.bash-completion
	rm -rf $HOME/.config/lsd
	echo "alias ls='ls --color'" >> $HOME/.bashrc
	echo "*** Please remove 'alias ls=lsd' from $HOME/.bashrc manually ***"
}

# exa: ls
EXA_VER=0.10.1
EXA_FN=exa-linux-x86_64-musl-v${EXA_VER}.zip
EXA_URL="https://github.com/ogham/exa/releases/download/v${EXA_VER}/${EXA_FN}"

download_exa() {
	wget -c -O $DL_CACHE/$EXA_FN "$EXA_URL"
}
clean_exa() {
	rm -f $DL_CACHE/$EXA_FN
}
install_exa() {
	install_zip $EXA_FN bin/exa completions/exa.bash
	exa /
}
remove_exa() {
	remove exa exa.bash
}

# broot: tree, cd, ls, mv, cp, rm, mkdir, du, etc.
BROOT_FN=broot
BROOT_URL="https://dystroy.org/broot/download/x86_64-unknown-linux-musl/${BROOT_FN}"
BROOT_COMPLETION_BR_FN=br.bash
BROOT_COMPLETION_BR_URL="https://dystroy.org/broot/download/completion/${BROOT_COMPLETION_BR_FN}"
BROOT_COMPLETION_BROOT_FN=broot.bash
BROOT_COMPLETION_BROOT_URL="https://dystroy.org/broot/download/completion/${BROOT_COMPLETION_BROOT_FN}"

download_broot() {
	wget -c -O $DL_CACHE/$BROOT_FN "$BROOT_URL"
	wget -c -O $DL_CACHE/$BROOT_COMPLETION_BR_FN "$BROOT_COMPLETION_BR_URL"
	wget -c -O $DL_CACHE/$BROOT_COMPLETION_BROOT_FN "$BROOT_COMPLETION_BROOT_URL"
}
clean_broot() {
	rm -f $DL_CACHE/$BROOT_FN
	rm -f $DL_CACHE/$BROOT_COMPLETION_BR_FN
	rm -f $DL_CACHE/$BROOT_COMPLETION_BROOT_FN
}
install_broot() {
	cp $DL_CACHE/$BROOT_FN $TARGET/bin
	chmod +x $TARGET/bin/$BROOT_FN
	cp $DL_CACHE/$BROOT_COMPLETION_BR_FN $BASH_COMPLETION_D/
	cp $DL_CACHE/$BROOT_COMPLETION_BROOT_FN $BASH_COMPLETION_D/
}
init_broot() {
	stty cols 80 rows 24 || : # Kylin Linux AS 10 SP1 B20 need this, but on Ubuntu 20.04 this will fail
	broot --install
}
remove_broot() {
	remove $BROOT_FN $BROOT_COMPLETION_BR_FN $BROOT_COMPLETION_BROOT_FN
	rm -rf $HOME/.config/broot
	rm -rf $HOME/.local/share/broot
	echo "*** Please remove broot from $HOME/.bashrc and $HOME/.bash_profile manually ***"
}

# bat: cat
BAT_VER=0.18.2
BAT_FN=bat-v${BAT_VER}-x86_64-unknown-linux-musl.tar.gz
BAT_URL="https://github.com/sharkdp/bat/releases/download/v${BAT_VER}/${BAT_FN}"

download_bat() {
	wget -c -O $DL_CACHE/$BAT_FN "$BAT_URL"
}
clean_bat() {
	rm -f $DL_CACHE/$BAT_FN
}
install_bat() {
	install_tar $BAT_FN 1 bat autocomplete/bat.bash
	bat /etc/hosts
}
remove_bat() {
	remove bat bat.bash
}

# ripgrep: grep
RG_VER=13.0.0
RG_FN=ripgrep-${RG_VER}-x86_64-unknown-linux-musl.tar.gz
RG_URL="https://github.com/BurntSushi/ripgrep/releases/download/${RG_VER}/${RG_FN}"

download_rg() {
	wget -c -O $DL_CACHE/$RG_FN "$RG_URL"
}
clean_rg() {
	rm -f $DL_CACHE/$RG_FN
}
install_rg() {
	install_tar $RG_FN 1 rg complete/rg.bash
	rg local /etc/hosts
}
remove_rg() {
	remove rg rg.bash
}

# fd: find
FD_VER=8.2.1
FD_FN=fd-v${FD_VER}-x86_64-unknown-linux-musl.tar.gz
FD_URL="https://github.com/sharkdp/fd/releases/download/v${FD_VER}/${FD_FN}"

download_fd() {
	wget -c -O $DL_CACHE/$FD_FN "$FD_URL"
}
clean_fd() {
	rm -f $DL_CACHE/$FD_FN
}
install_fd() {
	install_tar $FD_FN 1 fd autocomplete/fd.bash-completion
	fd hosts /etc
}
remove_fd() {
	remove fd fd.bash-completion
}

# delta: diff
DELTA_VER=0.8.3
DELTA_FN=delta-${DELTA_VER}-x86_64-unknown-linux-musl.tar.gz
DELTA_URL="https://github.com/dandavison/delta/releases/download/${DELTA_VER}/${DELTA_FN}"

download_delta() {
	wget -c -O $DL_CACHE/$DELTA_FN "$DELTA_URL"
}
clean_delta() {
	rm -f $DL_CACHE/$DELTA_FN
}
install_delta() {
	install_tar $DELTA_FN 1 delta
	delta /etc/hosts /etc/hosts
}
init_delta() {
	cat $PWD/delta.gitconfig >> $HOME/.gitconfig
}
remove_delta() {
	remove delta
	echo "*** Please remove delta in $HOME/.gitconfig manually ***"
}

# duf: df
DUF_VER=0.6.2
DUF_FN=duf_${DUF_VER}_linux_x86_64.tar.gz
DUF_URL="https://github.com/muesli/duf/releases/download/v${DUF_VER}/${DUF_FN}"

download_duf() {
	wget -c -O $DL_CACHE/$DUF_FN "$DUF_URL"
}
clean_duf() {
	rm -f $DL_CACHE/$DUF_FN
}
install_duf() {
	install_tar $DUF_FN 0 duf
	# On Ubuntu 20.04 host timeout doesn't work so disable it
	#timeout 5 duf || : # duf won't exit on Kylin Linux AS 10 SP1 B20 while docker build
}
remove_duf() {
	remove duf
}

# dust: du
DUST_VER=0.6.1
DUST_FN=dust-v${DUST_VER}-x86_64-unknown-linux-musl.tar.gz
DUST_URL="https://github.com/bootandy/dust/releases/download/v${DUST_VER}/${DUST_FN}"

download_dust() {
	wget -c -O $DL_CACHE/$DUST_FN "$DUST_URL"
}
clean_dust() {
	rm -f $DL_CACHE/$DUST_FN
}
install_dust() {
	install_tar $DUST_FN 1 dust
	dust
}
remove_dust() {
	remove dust
}

# dua: du
DUA_VER=2.14.4
DUA_FN=dua-v${DUA_VER}-x86_64-unknown-linux-musl.tar.gz
DUA_URL="https://github.com/Byron/dua-cli/releases/download/v${DUA_VER}/${DUA_FN}"

download_dua() {
	wget -c -O $DL_CACHE/$DUA_FN "$DUA_URL"
}
clean_dua() {
	rm -f $DL_CACHE/$DUA_FN
}
install_dua() {
	install_tar $DUA_FN 1 dua
	dua
}
remove_dua() {
	remove dua
}

# procs: ps
PROCS_VER=0.11.9
PROCS_FN=procs-v${PROCS_VER}-x86_64-lnx.zip
PROCS_URL="https://github.com/dalance/procs/releases/download/v${PROCS_VER}/${PROCS_FN}"

download_procs() {
	wget -c -O $DL_CACHE/$PROCS_FN "$PROCS_URL"
}
clean_procs() {
	rm -f $DL_CACHE/$PROCS_FN
}
install_procs() {
	install_zip $PROCS_FN procs
	procs
}
remove_procs() {
	remove procs
}

# sd: sed
SD_VER=0.7.6
SD_FN=sd-v${SD_VER}-x86_64-unknown-linux-musl
SD_URL="https://github.com/chmln/sd/releases/download/v${SD_VER}/${SD_FN}"

download_sd() {
	wget -c -O $DL_CACHE/$SD_FN "$SD_URL"
}
clean_sd() {
	rm -f $DL_CACHE/$SD_FN
}
install_sd() {
	cp $DL_CACHE/$SD_FN $TARGET/bin/sd
	chmod +x $TARGET/bin/sd
	# musl version seems to be the same as normal version
	# so it doesn't work on CentOS 7 and Alpine
	sd --version || :
}
remove_sd() {
	remove sd
}

# choose: cut & awk
CHOOSE_VER=1.3.3
CHOOSE_FN=choose-x86_64-unknown-linux-musl
CHOOSE_URL="https://github.com/theryangeary/choose/releases/download/v${CHOOSE_VER}/${CHOOSE_FN}"

download_choose() {
	wget -c -O $DL_CACHE/$CHOOSE_FN "$CHOOSE_URL"
}
clean_choose() {
	rm -f $DL_CACHE/$CHOOSE_FN
}
install_choose() {
	cp $DL_CACHE/$CHOOSE_FN $TARGET/bin/choose
	chmod +x $TARGET/bin/choose
	echo 'a b c' | choose 1
}
remove_choose() {
	remove choose
}

# jq: json processor
JQ_VER=1.6
JQ_FN=jq-linux64
JQ_URL="https://github.com/stedolan/jq/releases/download/jq-${JQ_VER}/${JQ_FN}"

download_jq() {
	wget -c -O $DL_CACHE/$JQ_FN "$JQ_URL"
}
clean_jq() {
	rm -f $DL_CACHE/$JQ_FN
}
install_jq() {
	cp $DL_CACHE/$JQ_FN $TARGET/bin/jq
	chmod +x $TARGET/bin/jq
	echo '{"foo": 0}' | jq .
}
remove_jq() {
	remove jq
}

# xh: curl & httpie
XH_VER=0.11.0
XH_FN=xh-v${XH_VER}-x86_64-unknown-linux-musl.tar.gz
XH_URL="https://github.com/ducaale/xh/releases/download/v${XH_VER}/${XH_FN}"

download_xh() {
	wget -c -O $DL_CACHE/$XH_FN "$XH_URL"
}
clean_xh() {
	rm -f $DL_CACHE/$XH_FN
}
install_xh() {
	install_tar $XH_FN 1 xh completions/xh.bash
	xh baidu.com || : # may fail when not connected to internet
}
remove_xh() {
	remove xh xh.bash
}

# curlie: curl & httpie
CURLIE_VER=1.6.0
CURLIE_FN=curlie_${CURLIE_VER}_linux_amd64.tar.gz
CURLIE_URL="https://github.com/rs/curlie/releases/download/v${CURLIE_VER}/${CURLIE_FN}"

download_curlie() {
	wget -c -O $DL_CACHE/$CURLIE_FN "$CURLIE_URL"
}
clean_curlie() {
	rm -f $DL_CACHE/$CURLIE_FN
}
install_curlie() {
	install_tar $CURLIE_FN 0 curlie
	curlie baidu.com || : # may fail when not connected to internet
}
remove_curlie() {
	remove curlie
}

# bottom: top & sysstat
BTM_VER=0.6.3
BTM_FN=bottom_x86_64-unknown-linux-musl.tar.gz
BTM_URL="https://github.com/ClementTsang/bottom/releases/download/${BTM_VER}/${BTM_FN}"

download_btm() {
	wget -c -O $DL_CACHE/$BTM_FN "$BTM_URL"
}
clean_btm() {
	rm -f $DL_CACHE/$BTM_FN
}
install_btm() {
	install_tar $BTM_FN 0 btm completion/btm.bash
	btm --version
}
remove_btm() {
	remove btm btm.bash
}

# orgalorg: parallel ssh
ORGALORG_VER=1.1.1
ORGALORG_FN=orgalorg_${ORGALORG_VER}_linux_amd64.tar.gz
ORGALORG_URL="https://github.com/reconquest/orgalorg/releases/download/${ORGALORG_VER}/${ORGALORG_FN}"

download_orgalorg() {
	wget -c -O $DL_CACHE/$ORGALORG_FN "$ORGALORG_URL"
}
clean_orgalorg() {
	rm -f $DL_CACHE/$ORGALORG_FN
}
install_orgalorg() {
	install_tar $ORGALORG_FN 0 orgalorg
	orgalorg --version
}
remove_orgalorg() {
	remove orgalorg
}

# dive: a tool for exploring each layer in a docker image
DIVE_VER=0.10.0
DIVE_FN=dive_${DIVE_VER}_linux_amd64.tar.gz
DIVE_URL="https://github.com/wagoodman/dive/releases/download/v${DIVE_VER}/${DIVE_FN}"

download_dive() {
	wget -c -O $DL_CACHE/$DIVE_FN "$DIVE_URL"
}
clean_dive() {
	rm -f $DL_CACHE/$DIVE_FN
}
install_dive() {
	install_tar $DIVE_FN 0 dive
	dive --version
}
remove_dive() {
	remove dive
}

# micro: a modern and intuitive terminal-based text editor
MICRO_VER=2.0.10
MICRO_FN=micro-${MICRO_VER}-linux64-static.tar.gz
MICRO_URL="https://github.com/zyedidia/micro/releases/download/v${MICRO_VER}/${MICRO_FN}"

download_micro() {
	wget -c -O $DL_CACHE/$MICRO_FN "$MICRO_URL"
}
clean_micro() {
	rm -f $DL_CACHE/$MICRO_FN
}
install_micro() {
	install_tar $MICRO_FN 1 micro
	micro --version
}
remove_micro() {
	remove micro
}

# vimrc
init_vimrc() {
	cat $PWD/vimrc >> $HOME/.vimrc
}
remove_vimrc() {
	echo "*** Please remove unwanted settings in $HOME/.vimrc manually ***"
}
